﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;

namespace Du_Lich
{
    class HamTao
    {

        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter da = null;
        

        string strConn = ConfigurationManager.ConnectionStrings["DuLich"].ConnectionString.ToString();

        public void MoKetNoi()
        {
            conn = new SqlConnection(strConn);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
        }

        public void DongKetNoi()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        public void ThemNhanVien(string manv, string hoten, string user, string pass, string ngaysinh, string diachi, string phai, string chucvu, string sodt, string ngayvaolam, string maquyen)
        {
            try
            {
                conn = new SqlConnection(strConn);
                string sqlInsert = "Insert NHANVIEN values(@manv,@hoten,@user,@pass,@ngaysinh,@diachi,@gioitinh,@chucvu,@sodt,@ngayvaolam,@maquyen)";
                cmd = new SqlCommand(sqlInsert, conn);

                cmd.Parameters.AddWithValue("@manv", manv);
                cmd.Parameters.AddWithValue("@hoten", hoten);
                cmd.Parameters.AddWithValue("@user", user);
                cmd.Parameters.AddWithValue("@pass", pass);
                cmd.Parameters.AddWithValue("@ngaysinh", ngaysinh);
                cmd.Parameters.AddWithValue("@diachi", diachi);
                cmd.Parameters.AddWithValue("@gioitinh", phai);
                cmd.Parameters.AddWithValue("@chucvu", chucvu);
                cmd.Parameters.AddWithValue("@sodt", sodt);
                cmd.Parameters.AddWithValue("@ngayvaolam", ngayvaolam);
                cmd.Parameters.AddWithValue("@maquyen", maquyen);

                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void XoaNhanVien(string manv)
        {
            string sqlDelete = "Delete NHANVIEN where MaNV=@manv";
            cmd = new SqlCommand(sqlDelete, conn);
            cmd.Parameters.AddWithValue("@manv", manv);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

       public DataTable ThongTinNhanVien()
       {
           string sqlThongTin = "Select * from NHANVIEN";
           cmd = new SqlCommand(sqlThongTin, conn);
           SqlDataAdapter da = new SqlDataAdapter(sqlThongTin, conn);
           DataTable dt = new DataTable();
           da.Fill(dt);
           return dt;
       }
    }
}
